package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnStartNode extends Shape {

	public static final String KEY_NODE_START = "start";

	public static final String KEY_PROPERTY_START_NODE_NAME_KEY = "_property_name";

	public static final String KEY_PROPERTY_START_NODE_DESCRITPION = "_property_description";

	public static final String KEY_PROPERTY_START_NODE_PARAMETERS = "_property_parameters";

	public static final String SHOW_START_NODE_NAME = "name";

	public static final String SHOW_START_NODE_DESCRITPION = "description";

	public static final String SHOW_START_NODE_PARAMETERS = "parameters";

	public static final String SHOW_START_NODE_TRANSACTION = "transaction";

	public static final String SHOW_START_NODE_TRANSACTIONAUTOROLLBACK = "autorollback";

	public static final String AUTOROLLBACK_PROPERTY = "autorollback_property";

	private static final long serialVersionUID = 2946279234083480438L;

	private String autorollback = SelectionCandidate.falseOrTrue[0];

	private Image image = Activator.getImage(Images.START_ICON);

	public SaturnStartNode() {
		super.setName(Messages.getString("SaturnStartNode.start")); //$NON-NLS-1$
	}

	@Override
	public Image getIcon() {
		return image;
	}

	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node startNode = SaturnW3CUtil.createChildNode(root, KEY_NODE_START,
				null);

		SaturnW3CUtil.createChildNode(startNode, SHOW_START_NODE_NAME, super
				.getName());

		this.createGlobalTransaction(startNode);
		super.createParemeters(startNode);

		return startNode;
	}

	/**
	 * 创建Start的globalTransaction
	 * 
	 */
	public void createGlobalTransaction(Node node) {
		Node transactionNode = SaturnW3CUtil.createChildNode(node,
				SHOW_START_NODE_TRANSACTION, null);

		SaturnW3CUtil.createAttribute(transactionNode,
				SHOW_START_NODE_TRANSACTIONAUTOROLLBACK, this.autorollback);
	}


	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		return null;
	}

	@Override
	protected void setImage(Image image) {
		this.image = image;
	}

	public String getAutorollback() {
		return autorollback;
	}

	public void setAutorollback(String autorollback) {
		if (!this.autorollback.equals(autorollback)) {
			this.autorollback = autorollback;
			firePropertyChange(AUTOROLLBACK_PROPERTY, null, autorollback);
		}
	}
	
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		Map<String, List<?>> summm = new HashMap<String, List<?>>();
		List<SaturnParameter> parameters = getParameters();
		String name = getName();
		Parameters para = new Parameters();
		
		para.setName(getName());
		para.setId(getId());
		para.setDescription(getDescription());
		para.setAutorollback(getAutorollback());
	 	basicvaluse.add(para);
		
		para.setInfourl( name + "-start.html");
		para.setInfoname( name );
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.StartNode")); //$NON-NLS-1$
		para.setImagename("start");
		summm.put("parameters", parameters);
		summm.put("basicvaluse", basicvaluse);
		String templatePath = "template/export/logic/start";
		String Path = filePath + File.separator + name + "-start.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,summm, Path);
		
		return para;
	}
}
